sources <- c("ESPN", "FantasyData", "FantasyPros", "FantasySharks", 
             "FantasyFootballNerd",
             "RTSports", "Walterfootball")

scrape <- scrape_data(src = sources,
                      pos = c('QB', 'RB', 'WR', 'TE', 'DST'),
                      season = 2020, 
                      week = week)

Simulation Time!

n_sims <- 10000

tic()
sim_lu <- map_df(1:n_sims, generate_lineup) %>%
  rename(pts_base = points) %>%  
  mutate(position = factor(position, 
                           levels = c("QB", "RB", "WR", "TE", "DST"))) %>% 
  select(lineup, Name, team, position, pts_base, pts_pred, sd_pts, Salary)
toc()
## 796.445 sec elapsed

Results

ggplotly(sim_lu %>% 
           group_by(Name, position, Salary) %>% 
           dplyr::summarize(lu = n_distinct(lineup)) %>% 
           ungroup() %>% 
           group_by(position) %>% 
           top_n(10, lu) %>% 
           ungroup() %>% 
           arrange(position, desc(lu)) %>% 
           mutate(Name = factor(Name),
                  Name = fct_reorder(Name, lu),
                  pct = round(lu / n_sims, 3) * 100) %>% 
           ggplot(aes(x = Name, y = pct, fill = Salary,
                      text = paste(Name, "in", lu, "lineups with", Salary, "salary"))) +
           geom_bar(stat = "identity") +
           facet_wrap(~position, ncol = 2, scales = "free_y") +
           coord_flip() +
           scale_fill_viridis_c() +
           xlab("") +
           ylab("Lineups (thousands)") +
           ggtitle("Top 10 Players By Position")) %>% 
  ggplotly(tooltip = "text")
plyr_lu <- sim_lu %>%
  group_by(Name, position) %>%
  dplyr::summarize(lu=n_distinct(lineup)) %>%
  ungroup() 

ggplotly(projections %>% 
  filter(avg_type=='weighted') %>%
  mutate(Name = ifelse(pos=="DST", last_name, paste(first_name, last_name))) %>%
  inner_join(fan_duel, by = c("Name", "position")) %>%
  select(Name, team, position, points, Salary, sd_pts) %>%
  left_join(plyr_lu, by='Name') %>%
  replace_na(list(lu=0)) %>%
  mutate(lu_bin=ifelse(lu==0, '0 Lineups', '>=1 Lineups'),
         lu_5=cut(lu,5, labels = FALSE)) %>%
  ggplot(aes(x=Salary, y=points, color=lu_bin, size=sd_pts, text=Name)) +
  geom_point() +
  theme_minimal() +
  scale_color_manual(values = c('red', 'blue'), name="") +
  geom_smooth(inherit.aes = FALSE, aes(x=Salary, y=points), method = 'lm') +
  ylab('Projected Points') +
  xlab('Salary') +
  ggtitle('Who makes it into Optimized Lineups?') +
  scale_x_continuous(labels=scales::dollar))
lu_df <- sim_lu %>%
  group_by(lineup) %>%
  dplyr::summarize(lineup_pts=sum(pts_pred),
                   lineup_sd=sum(sd_pts)) %>%
  ungroup()

pto <- psel(lu_df, low(lineup_sd) * high(lineup_pts))


ggplotly(ggplot(lu_df, aes(y=lineup_pts, x=lineup_sd, text = paste0("Lineup: ", lineup))) +
  geom_point() +
  geom_point(data=pto, size=5) +
  ylab('Lineup Points') +
  xlab('Lineup Points St Dev') +
  ggtitle('Lineup Points vs. Uncertainty',
          subtitle = 'Pareto Lineups Bolded'))
psel(lu_df, low(lineup_sd) * high(lineup_pts)) %>%
  left_join(sim_lu, by='lineup') %>%
  group_by(lineup) %>%
  arrange(lineup_pts, position, desc(Salary)) %>%
  select(lineup, lineup_pts, lineup_sd, Name, team, position, pts_pred, sd_pts, Salary) %>%
  mutate_at(vars(lineup_pts, lineup_sd, pts_pred, sd_pts), function(x) round(x, 2)) %>%
  knitr::kable() %>%
  kable_styling(fixed_thead = T) %>%
  column_spec(1:3, bold=TRUE) %>%
  collapse_rows(columns = 1:3, valign = 'top') %>%
  scroll_box(height = '700px', width = '100%')
lineup lineup_pts lineup_sd Name team position pts_pred sd_pts Salary
7124 129.65 3.61 Lamar Jackson BAL QB 23.67 1.37 8100
James Conner PIT RB 17.18 0.27 8200
Josh Jacobs LVR RB 15.86 0.04 7700
David Montgomery CHI RB 13.68 0.06 5900
Julio Jones ATL WR 15.93 0.09 8200
Stefon Diggs BUF WR 15.85 0.31 7600
Robby Anderson CAR WR 12.46 0.37 6600
Kyle Rudolph MIN TE 7.39 0.36 4400
Atlanta Falcons ATL DST 7.62 0.74 3300
3118 131.60 4.15 Russell Wilson SEA QB 24.55 0.16 9000
James Conner PIT RB 17.07 0.27 8200
Josh Jacobs LVR RB 15.86 0.04 7700
David Montgomery CHI RB 13.50 0.06 5900
Stefon Diggs BUF WR 15.80 0.31 7600
Robby Anderson CAR WR 12.79 0.37 6600
Tim Patrick DEN WR 10.28 0.52 5300
Darren Waller LVR TE 14.10 1.68 6400
Atlanta Falcons ATL DST 7.65 0.74 3300
3937 132.38 5.18 Russell Wilson SEA QB 24.28 0.16 9000
Dalvin Cook MIN RB 19.88 1.28 9300
Josh Jacobs LVR RB 15.96 0.04 7700
David Montgomery CHI RB 13.56 0.06 5900
Stefon Diggs BUF WR 16.14 0.31 7600
Robby Anderson CAR WR 12.68 0.37 6600
DeVante Parker MIA WR 11.98 0.17 6300
Jason Witten LVR TE 10.91 2.05 4300
Atlanta Falcons ATL DST 6.99 0.74 3300
6605 133.40 5.41 Russell Wilson SEA QB 24.42 0.16 9000
James Conner PIT RB 17.28 0.27 8200
Josh Jacobs LVR RB 15.89 0.04 7700
David Montgomery CHI RB 13.75 0.06 5900
Stefon Diggs BUF WR 15.63 0.31 7600
Robby Anderson CAR WR 12.78 0.37 6600
DeVante Parker MIA WR 12.02 0.17 6300
Jimmy Graham CHI TE 13.86 3.28 5300
Atlanta Falcons ATL DST 7.76 0.74 3300
111 135.03 5.64 Deshaun Watson HOU QB 23.00 0.12 8300
James Conner PIT RB 17.16 0.27 8200
Chase Edmonds ARI RB 16.85 1.86 6700
David Montgomery CHI RB 13.60 0.06 5900
Julio Jones ATL WR 15.83 0.09 8200
Stefon Diggs BUF WR 16.39 0.31 7600
Tyler Lockett SEA WR 18.16 1.84 7400
Kyle Rudolph MIN TE 7.13 0.36 4400
Atlanta Falcons ATL DST 6.91 0.74 3300
836 135.23 5.71 Russell Wilson SEA QB 24.75 0.16 9000
James Conner PIT RB 17.13 0.27 8200
Justin Jackson LAC RB 12.15 0.48 5900
David Montgomery CHI RB 13.65 0.06 5900
Stefon Diggs BUF WR 15.24 0.31 7600
Tyler Lockett SEA WR 17.26 1.84 7400
DeVante Parker MIA WR 12.32 0.17 6300
Darren Waller LVR TE 14.11 1.68 6400
Atlanta Falcons ATL DST 8.62 0.74 3300
9856 135.37 5.73 Deshaun Watson HOU QB 23.09 0.12 8300
Dalvin Cook MIN RB 24.02 1.28 9300
Justin Jackson LAC RB 12.95 0.48 5900
David Montgomery CHI RB 13.69 0.06 5900
Stefon Diggs BUF WR 15.71 0.31 7600
Terry McLaurin WAS WR 14.31 1.28 7200
Robby Anderson CAR WR 12.45 0.37 6600
Noah Fant DEN TE 11.36 1.09 5800
Atlanta Falcons ATL DST 7.79 0.74 3300
489 136.49 6.14 Russell Wilson SEA QB 24.60 0.16 9000
James Conner PIT RB 17.00 0.27 8200
Chase Edmonds ARI RB 17.70 1.86 6700
David Montgomery CHI RB 13.59 0.06 5900
Julio Jones ATL WR 15.71 0.09 8200
Stefon Diggs BUF WR 15.78 0.31 7600
DeVante Parker MIA WR 12.27 0.17 6300
Greg Olsen SEA TE 12.86 2.48 4800
Atlanta Falcons ATL DST 6.99 0.74 3300
5548 138.44 6.81 Russell Wilson SEA QB 24.64 0.16 9000
James Conner PIT RB 17.08 0.27 8200
David Montgomery CHI RB 13.56 0.06 5900
Stefon Diggs BUF WR 15.97 0.31 7600
Tyler Lockett SEA WR 18.16 1.84 7400
Robby Anderson CAR WR 13.25 0.37 6600
Danny Amendola DET WR 13.09 1.38 5300
Darren Waller LVR TE 13.56 1.68 6400
Atlanta Falcons ATL DST 9.12 0.74 3300
196 139.19 7.66 Kyler Murray ARI QB 23.78 1.57 8600
Dalvin Cook MIN RB 22.35 1.28 9300
Josh Jacobs LVR RB 15.89 0.04 7700
David Montgomery CHI RB 13.50 0.06 5900
Stefon Diggs BUF WR 15.89 0.31 7600
Darius Slayton NYG WR 11.93 0.61 5900
Danny Amendola DET WR 11.28 1.38 5300
Darren Waller LVR TE 15.11 1.68 6400
Atlanta Falcons ATL DST 9.44 0.74 3300
28 139.82 8.32 Russell Wilson SEA QB 24.63 0.16 9000
Dalvin Cook MIN RB 21.30 1.28 9300
Justin Jackson LAC RB 12.10 0.48 5900
David Montgomery CHI RB 13.60 0.06 5900
Stefon Diggs BUF WR 15.71 0.31 7600
Tyler Lockett SEA WR 16.64 1.84 7400
DeVante Parker MIA WR 12.22 0.17 6300
Jimmy Graham CHI TE 14.79 3.28 5300
Atlanta Falcons ATL DST 8.82 0.74 3300
3630 140.47 8.82 Josh Allen BUF QB 23.11 1.23 8200
James Conner PIT RB 17.44 0.27 8200
Josh Jacobs LVR RB 15.83 0.04 7700
David Montgomery CHI RB 13.67 0.06 5900
Stefon Diggs BUF WR 16.03 0.31 7600
Curtis Samuel CAR WR 11.77 1.73 5600
Cole Beasley BUF WR 18.45 3.28 5300
Travis Kelce KCC TE 16.68 1.15 8000
Atlanta Falcons ATL DST 7.48 0.74 3300
2158 144.47 8.94 Russell Wilson SEA QB 24.49 0.16 9000
Dalvin Cook MIN RB 22.14 1.28 9300
James Conner PIT RB 17.20 0.27 8200
David Montgomery CHI RB 13.47 0.06 5900
Stefon Diggs BUF WR 16.03 0.31 7600
Danny Amendola DET WR 12.06 1.38 5300
Nelson Agholor LVR WR 18.38 3.06 5000
Darren Waller LVR TE 12.14 1.68 6400
Atlanta Falcons ATL DST 8.57 0.74 3300
9640 146.38 10.38 Josh Allen BUF QB 24.56 1.23 8200
Dalvin Cook MIN RB 20.72 1.28 9300
Chase Edmonds ARI RB 20.82 1.86 6700
David Montgomery CHI RB 13.54 0.06 5900
Stefon Diggs BUF WR 15.92 0.31 7600
DeVante Parker MIA WR 12.02 0.17 6300
Kenny Stills HOU WR 10.52 1.65 4600
Darren Waller LVR TE 15.17 1.68 6400
Pittsburgh Steelers PIT DST 13.10 2.15 5000
6043 151.17 12.05 Kyler Murray ARI QB 25.78 1.57 8600
Dalvin Cook MIN RB 20.77 1.28 9300
James Conner PIT RB 17.15 0.27 8200
David Montgomery CHI RB 13.70 0.06 5900
Stefon Diggs BUF WR 16.00 0.31 7600
John Brown BUF WR 11.40 1.49 5500
Kenny Stills HOU WR 11.15 1.65 4600
Jimmy Graham CHI TE 20.59 3.28 5300
Pittsburgh Steelers PIT DST 14.62 2.15 5000
9719 151.76 13.72 Russell Wilson SEA QB 24.45 0.16 9000
Christian McCaffrey CAR RB 22.44 2.41 9500
Chase Edmonds ARI RB 17.53 1.86 6700
David Montgomery CHI RB 13.61 0.06 5900
Stefon Diggs BUF WR 16.22 0.31 7600
Tyler Lockett SEA WR 20.38 1.84 7400
Nelson Agholor LVR WR 12.62 3.06 5000
Jimmy Graham CHI TE 16.38 3.28 5300
Atlanta Falcons ATL DST 8.12 0.74 3300
7831 151.82 15.31 Chase Daniel DET QB 37.20 8.06 6000
Dalvin Cook MIN RB 20.06 1.28 9300
David Montgomery CHI RB 13.60 0.06 5900
Alfred Morris NYG RB 13.09 2.74 4500
DeAndre Hopkins ARI WR 16.11 0.36 8800
Julio Jones ATL WR 15.76 0.09 8200
Stefon Diggs BUF WR 15.97 0.31 7600
Darren Waller LVR TE 12.70 1.68 6400
Atlanta Falcons ATL DST 7.34 0.74 3300
5464 152.82 15.69 Geno Smith SEA QB 35.42 9.51 6000
Dalvin Cook MIN RB 20.97 1.28 9300
Chase Edmonds ARI RB 17.29 1.86 6700
David Montgomery CHI RB 13.52 0.06 5900
Julio Jones ATL WR 15.92 0.09 8200
Stefon Diggs BUF WR 16.22 0.31 7600
DeVante Parker MIA WR 11.97 0.17 6300
Darren Waller LVR TE 13.60 1.68 6400
Atlanta Falcons ATL DST 7.89 0.74 3300
1318 152.83 16.07 Geno Smith SEA QB 32.61 9.51 6000
Dalvin Cook MIN RB 20.36 1.28 9300
Josh Jacobs LVR RB 15.94 0.04 7700
David Montgomery CHI RB 13.52 0.06 5900
Stefon Diggs BUF WR 16.23 0.31 7600
Tyler Lockett SEA WR 18.89 1.84 7400
DeVante Parker MIA WR 12.25 0.17 6300
Darren Waller LVR TE 15.02 1.68 6400
Detroit Lions DET DST 8.02 1.19 3200
6287 158.69 17.74 Chase Daniel DET QB 39.49 8.06 6000
Dalvin Cook MIN RB 21.22 1.28 9300
David Montgomery CHI RB 13.49 0.06 5900
Tyreek Hill KCC WR 17.16 1.48 8300
Adam Thielen MIN WR 14.89 0.49 7700
Stefon Diggs BUF WR 15.12 0.31 7600
Darren Waller LVR TE 14.39 1.68 6400
Greg Olsen SEA TE 11.93 2.48 4800
Chicago Bears CHI DST 10.99 1.91 4000
668 161.53 18.05 Geno Smith SEA QB 40.26 9.51 6000
Dalvin Cook MIN RB 20.29 1.28 9300
Chase Edmonds ARI RB 14.85 1.86 6700
David Montgomery CHI RB 13.56 0.06 5900
Julio Jones ATL WR 15.70 0.09 8200
Stefon Diggs BUF WR 15.75 0.31 7600
Nelson Agholor LVR WR 16.98 3.06 5000
Travis Kelce KCC TE 15.12 1.15 8000
Atlanta Falcons ATL DST 9.02 0.74 3300